<?php
namespace app\common\job;

use think\queue\Job;

/**
 * 兑换券异步操作
 */
class CouponExchangeAutoOper
{

    /**
     * 未使用的优惠券过期处理
     */
    public function UnusedCouponExchangeOverdue(Job $job, $data)
    {
        try {
            // 重新查询订单
            \think\Log::write('queue-' . get_class() . '-UnusedCouponExchangeOverdue' . '：执行成功');
            $user_coupon_exchange = model('app\common\model\UserCouponExchangeModel')->get($data['id']);
            if ($user_coupon_exchange && $user_coupon_exchange->status == 1) {
                \think\Db::transaction(function () use ($user_coupon_exchange, $data) {
                    if($user_coupon_exchange->end_time < time()) {
                        $user_coupon_exchange->status = 3;
                        $user_coupon_exchange->save();
                        \think\Log::write('queue-' . get_class() . '-UnusedCouponExchangeOverdue' . '：执行成功');
                    }
                });
            }
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-UnusedCouponExchangeOverdue' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }
}